#include "challengeroom.h"
#include <QListWidgetItem>
#include <QDebug>
#include <QMessageBox>

ChallengeRoom::ChallengeRoom(QWidget *parent) : QWidget(parent),ui(new Ui::ChallengeRoom){
    ui->setupUi(this);

    challengeDialog = new PassiveChallengeDialog(this);
    activeChallengeDialog = new ActiveChallengeDialog(this);

    messageBox = new QMessageBox(this);

    messageBox->setIcon(QMessageBox::Information);
    messageBox->setWindowTitle("Sorry...");
    messageBox->setStandardButtons(QMessageBox::Ok);
    messageBox->setStyleSheet(QString::fromUtf8("QMessageBox{\nbackground-color:#888c7e;\n}\nQLabel{\ncolor:#fff;\n}\nQPushButton{\npadding:10px 20px 10px 20px;\nborder:3px solid #78846a;\nbackground-color:#a5b592;\ncolor:#fff;\n}"));

    connect(ui->challenge,SIGNAL(clicked()),this,SLOT(prepChallenge()));
    connect(ui->logoutButton,SIGNAL(clicked()),this,SIGNAL(logout()));
    connect(challengeDialog,SIGNAL(accepted()),this,SLOT(acceptChallengeByPlayer()));
    connect(challengeDialog,SIGNAL(declined()),this,SLOT(declineChallengeByPlayer()));

    connect(activeChallengeDialog,SIGNAL(cancelChallenge()),this,SLOT(cancelChallengePrep()));
}

ChallengeRoom::~ChallengeRoom(){
    delete ui;
}

void ChallengeRoom::closeAll(){
    messageBox->close();
    activeChallengeDialog->close();
    challengeDialog->close();
}

//cover the cases where a passive challenge prompt is shown but the opponent has cancelled the challenge
void ChallengeRoom::handleNoChallenges(){
    if(challengeDialog->isVisible()){
        challengeDialog->close();

        messageBox->setText("<h2>"+opponent+" Has Canceled His Challenge</h2>");
        messageBox->exec();
    }

}

void ChallengeRoom::handleChallengeDeclined(){
    if(activeChallengeDialog->isVisible()){
        activeChallengeDialog->close();

        messageBox->setText("<h2>Your challenge was declined</h2>");
        messageBox->exec();
    }
}

void ChallengeRoom::prepChallenge(){
    QList<QListWidgetItem*> list = ui->playerListWidget->selectedItems();

    if(!list.isEmpty() && list.first()->text()!=""){
        emit challengePlayer(list.first()->text());
    }
}

void ChallengeRoom::challengedBy(QString player){
    if(!challengeDialog->isVisible()){
        opponent = player;
        challengeDialog->setChallenger(opponent);
        challengeDialog->exec();
    }
}

void ChallengeRoom::challengeDelivered(QString player){
    activeChallengeDialog->setChallenger(player);
    activeChallengeDialog->exec();
}

void ChallengeRoom::handleChallengeDeliveryFailure(QString player){
    if(!activeChallengeDialog->isVisible()){
        activeChallengeDialog->close();
        messageBox->setText("<h2>Challenge delivery to player "+player+" failed, please try again</h2>");

        messageBox->exec();
    }
}

void ChallengeRoom::cancelChallengePrep(){
    activeChallengeDialog->close();
    emit cancelChallenge();
}

void ChallengeRoom::acceptChallengeByPlayer(){
    challengeDialog->close();
    emit challengeByPlayerAccepted(opponent);
}

void ChallengeRoom::declineChallengeByPlayer(){
    challengeDialog->close();
    emit challengeByPlayerDeclined(opponent);
}

void ChallengeRoom::updatePlayerList(QList<QString> players){
    QList<QString>::Iterator iterator = players.begin();

    QString selected;

    QList<QListWidgetItem*> list = ui->playerListWidget->selectedItems();
    if(!list.isEmpty())
        selected = list.first()->text();

    ui->playerListWidget->clear();
    while(iterator != players.end()){
        QListWidgetItem* currentPlayer = new QListWidgetItem(*iterator,ui->playerListWidget);

        if(selected == *iterator)
            ui->playerListWidget->setItemSelected(currentPlayer,true);

        ++iterator;
    }
}
